package com.lijie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lijie.entity.TbLog;
import com.lijie.service.TbLogService;
import com.lijie.utilscommon.response.Result;
import com.lijie.vo.TbLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 操作日志表(TbLog)表控制层
 *
 * @author lijie
 * @since 2020-08-01 10:08:26
 */
@Api(value = "操作日志")
@CrossOrigin
@RestController
@RequestMapping("/loginservice/tbLog")
public class TbLogController {
    /**
     * 服务对象
     */
    @Resource
    private TbLogService tbLogService;

    @ApiOperation(value = "分页查询日志")
    @PostMapping("/Page/{current}/{size}")
    public Result Page(@PathVariable("current") Integer current, @PathVariable("size") Integer size, @RequestBody(required = false) TbLogVO tbLogVO) {
        Page<TbLog> page = new Page<>(current, size);
        QueryWrapper<TbLog> queryWrapper = getWrapper(tbLogVO);
        return Result.ok().data("total", tbLogService.findLogCount()).data("records", tbLogService.page(page, queryWrapper));
    }


    /**
     * 封装TbLogVO对象
     *
     * @param tbLogVO
     * @return
     */
    public QueryWrapper<TbLog> getWrapper(TbLogVO tbLogVO) {
        QueryWrapper<TbLog> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(tbLogVO)) {
            if (!StringUtils.isEmpty(tbLogVO.getUsername())) {
                queryWrapper.like("username", tbLogVO.getUsername());
            }
            if (!StringUtils.isEmpty(tbLogVO.getLocation())) {
                queryWrapper.like("location", tbLogVO.getLocation());
            }
            if (!StringUtils.isEmpty(tbLogVO.getIp())) {
                queryWrapper.like("ip", tbLogVO.getIp());
            }
        }
        return queryWrapper;
    }


    @ApiOperation(value = "逻辑删除日志操作信息")
    @DeleteMapping("/{id}")
    @CrossOrigin
    public Result delete(@PathVariable("id") Integer id) {

        boolean b = tbLogService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.error();
        }

    }


    @ApiOperation(value = "逻辑批量删除日志操作信息")
    @PostMapping("/deleteplus")
    @CrossOrigin
    public Result deleteplus(@RequestBody Integer[] ids) {
        List<Integer> id = new ArrayList<>();
        System.out.println(ids.length);
        for (int i = 0; i < ids.length; i++) {
            id.add(ids[i]);
            System.out.println(ids[i]);
        }
        boolean b = tbLogService.removeByIds(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


}